package org.cardcopier;

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import org.apache.log4j.Logger;
import org.cardcopier.file.CaseInsensitiveFileLocation;
import org.cardcopier.file.FileLocation;
import org.cardcopier.file.FileLocationComparator;
import org.cardcopier.file.FileState;
import org.cardcopier.ui.CardCopierUI;

public class CardCopier {
    
    public static final String DEFAULT_CONFIG_FILE = "flashCardCopier-config.xml";
    private static final Logger LOGGER = Logger.getLogger(CardCopier.class);
    
    private static CardCopierUI cardCopierUI;
    
    public static void main(String[] args) {
        LOGGER.info("FlashCardCopier started");
        ConfigFile configFile = new ConfigFile(new File(DEFAULT_CONFIG_FILE));
        ApplicationConfig config = new ApplicationConfig();
        String errorText = null;
        try {
            config = configFile.readConfig();
            CardCopierModel model = new CardCopierModel(config);

            cardCopierUI = new CardCopierUI(model, configFile);
            // Set message after the UI is created and listening for the events
            model.setMessageText(errorText);

            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    cardCopierUI.setVisible(true);
                    cardCopierUI.askForInitialPaths();
                }
            });
        
        } catch (IOException e) {
            errorText = "Failed to read application config from " + DEFAULT_CONFIG_FILE + " because: " + e.getMessage();
            LOGGER.error(errorText, e);
        } catch (Exception e) {
            errorText = "Application failed to start";
            LOGGER.error(errorText, e);
        }
    }
    
}
